//package com.turing.post.capitalaccounts.impl;
//
//import java.math.BigInteger;
//import java.util.Date;
//
//import javax.ejb.EJB;
//import javax.ejb.Local;
//import javax.ejb.Remote;
//import javax.ejb.Stateless;
//
//import com.turing.post.capitalaccounts.util.CapitalApplicationException;
//import com.turing.post.capitalaccounts.util.CapitalRespCode;
//import com.turing.post.capitalaccounts.util.SaleCapitalDetail;
//import com.turing.post.capitalaccounts.util.SaleCapitalDetailDao;
//import com.turing.post.capitalaccounts.util.SaleCreditAdjustBill;
//import com.turing.post.capitalaccounts.util.SaleCreditAdjustBillBusiness;
//import com.turing.post.capitalaccounts.util.SaleCreditAdjustBillDao;
//import com.turing.post.common.util.PCPSCommonDao;
//import com.turing.post.common.util.SysOpUser;
//import com.turing.post.common.util.SysOpUserDao;
//import com.turing.post.common.util.SysOperLog;
//import com.turing.post.common.util.SysOperLogDao;
//import com.turing.post.organization.util.IssueCompany;
//import com.turing.post.organization.util.IssueCompanyDao;
//import com.turing.post.organization.util.SaleBranch;
//import com.turing.post.organization.util.SaleBranchDao;
//import com.turing.post.organization.util.SysBranch;
//import com.turing.post.organization.util.SysBranchDao;
//import com.turing.post.util.PCPSApplicationException;
//import com.turing.post.util.PCPSWebInput;
//
//@Stateless(mappedName = "capitalaccounts.impl.SaleCreditAdjustBillBusinessImpl")
//@Remote
//@Local
//public class SaleCreditAdjustBillBusinessImpl implements
//		SaleCreditAdjustBillBusiness {
//
//	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
//	private IssueCompanyDao issueCompanyDaoImpl;
//
//	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
//	private SaleBranchDao saleBranchDaoImpl;
//
//	@EJB(mappedName = "capitalaccounts.impl.SaleCreditAdjustBillDaoImpl")
//	private SaleCreditAdjustBillDao saleCreditAjImpl;
//
//	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
//	private SysOpUserDao sysOpUserDaoImpl;
//	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
//	private SysOperLogDao sysOperLogDaoImpl;
//
//	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
//	private SysBranchDao sysBranchDaoImpl;
//
//	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
//	private PCPSCommonDao pcpsCommonDaoImpl;
//
//	@EJB(mappedName = "capitalaccounts.impl.SaleCapitalDetailDaoImpl")
//	private SaleCapitalDetailDao saleCapitalDetailDaoImpl;
//
//	@Override
//	public void insertSaleCreditAdJustBill(SaleCreditAdjustBill obj)
//			throws PCPSApplicationException {
//		// 业务检查
//		if (obj == null) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRMSG_NULLPOINTER,
//					CapitalRespCode.RC_ARGS_NULL, "参数为NULL");
//		}
//		SysOpUser sysOpUser = sysOpUserDaoImpl.getSysOpUserByOperId(obj
//				.getOperId());
//		// 判断操作员是否存在
//		if (sysOpUser == null) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRMSG_NULLPOINTER,
//					CapitalRespCode.OPER_NULL, "操作员为NULL");
//		}
//		SysBranch sysBranch = sysBranchDaoImpl.getSysBranchByBranchNo(sysOpUser
//				.getOperBranchNo());
//		// 判断操平台机构是否存在
//		if (sysBranch == null) {
//			throw new CapitalApplicationException(
//					CapitalRespCode.SYSBRANCH_NULL, "平台机构为NULL");
//		}
//		// // 判断操作员所属的平台机构是否为发卡机构
//		// if
//		// (!sysBranch.getBranchType().equals(SysBranch.SYSBRANCH_ISSUEBRANCH))
//		// {
//		// throw new CapitalApplicationException(CapitalRespCode.OPER_NULL,
//		// "操作员不属于发卡机构");
//		// }
//		// IssueBranch issueBranch =
//		// issueBranchDaoImpl.getIssueBranchByIssueBranchNo(sysBranch.getBranchNo());
//		// //判断发卡机构是否存在
//		// if (issueBranch == null) {
//		// throw new
//		// CapitalApplicationException(CapitalRespCode.ISSUEBRANCH_NULL,
//		// "发卡机构为NULL");
//		// }
//		// 判断发卡商机构是否为激活状态
//		// if (issueBranch.getStatus().equals(
//		// IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)) {
//		// throw new CapitalApplicationException(
//		// CapitalRespCode.SYSBRANCH_NOT_STATUS, "发卡机构未激活");
//		// }
//
//		SaleBranch saleBranch = saleBranchDaoImpl
//				.getSaleBranchBySaleBranchNo(obj.getSaleBranchNo());
//		// 判断销售机构是否存在
//		if (saleBranch == null) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRMSG_NULLPOINTER,
//					CapitalRespCode.SALEBRANCH_NULL, "销售机构为NULL");
//		}
//		if (saleBranch != null) {
//			// SaleCompany saleCompany =
//			// saleCompanyDaoImpl.getSaleCompanyBySaleCompanyNo(saleBranch.getSaleCompanyNo());
//			// 判断销售商是否存在
//			// if (saleCompany == null) {
//			// throw new
//			// CapitalApplicationException(CapitalRespCode.SALECOMPANY_NULL,
//			// "销售商为NULL");
//			// }
//			// 判断销售商是否为激活状态
//			if (saleBranch.getStatus().equals(
//					SaleBranch.SALEBRANCH_STATUS_NOTACTIVATED)) {
//				throw new CapitalApplicationException(
//						CapitalRespCode.SYSBRANCH_NOT_STATUS, "销售机构未激活!");
//			}
//			// 销售机构是否属于操作员的管辖机构
//			// if
//			// (!saleCompany.getIssueCompanyNo().equals(sysOpUser.getOperBranchNo().subst(0,
//			// 6))){
//			// throw new
//			// CapitalApplicationException(CapitalRespCode.SALEBRANCH_NOTBELONG_SYSOPUSER,
//			// "销售机构不属于操作员的管辖机构");
//			// }
//
//			IssueCompany issueCompany = issueCompanyDaoImpl
//					.getIssueCompanyByIssueCompanyNo(saleBranch
//							.getSaleBranchNo().subs(0, 6));
//			// 判断发卡商是否为激活状态
//			if (issueCompany.getActivate().equals(
//					IssueCompany.ISSUECOMPANY_ACTIVATE_NOTACTIVATED)) {
//				throw new CapitalApplicationException(
//						CapitalRespCode.SYSBRANCH_NOT_STATUS, "发卡商未激活");
//			}
//			// 判断是否为日终(交易)状态
//			if (issueCompany.getStatus().equals(
//					IssueCompany.ISSUECOMPANY_NORMAL_ENDDAY)) {
//				throw new CapitalApplicationException(
//						CapitalRespCode.NORMAL_ENDDAY, "发卡商为正在日终状态");
//			}
//		}
//
//		// 判断SaleBranch.Credit >= SaleBranch.CreditLowLimit
//		saleBranch.setCredit(saleBranch.getCredit().add(obj.getAmount()));
//		if (saleBranch.getCredit().compareTo(saleBranch.getCreditLowLimit()) < 0) {
//			throw new CapitalApplicationException(
//					CapitalRespCode.NORMAL_ENDDAY,
//					"信用额度SaleBranch.Credit < 预存款下限SaleBranch.CreditLowLimit");
//		}
//		PCPSWebInput webInput = new PCPSWebInput();
//		webInput.setOperId(obj.getOperId());
//		webInput.setOperBranchNo(obj.getSaleBranchNo());
//		saleBranchDaoImpl.updateSaleBranch(saleBranch);
//
//		Date curTime = pcpsCommonDaoImpl.getDatabaseSysTime();
//		// 插入销售机构信用度调帐
//		BigInteger saleCreditAdjustBillId = pcpsCommonDaoImpl
//				.getUniqueIntegerKey();
//		obj.setSaleCreditAdjustBillId(saleCreditAdjustBillId);
//		obj.setIssueCompanyNo(obj.getSaleBranchNo().subs(0, 6));
//		try {
//			obj.setTranDate(pcpsCommonDaoImpl.getSysDateFormatYMD());
//		} catch (Exception e) {
//			throw new CapitalApplicationException(
//					CapitalRespCode.NORMAL_ENDDAY, "转换日期出错!");
//		}
//		obj.setTranTime(curTime);
//		saleCreditAjImpl.insertSaleCreditAdjustBill(obj);
//
//		// 插入销售机构资金明细
//		SaleCapitalDetail saleCapitalDetail = new SaleCapitalDetail();
//		saleCapitalDetail.setSaleBranchNo(obj.getSaleBranchNo());
//		try {
//			saleCapitalDetail.setTranDate(pcpsCommonDaoImpl
//					.getSysDateFormatYMD());
//		} catch (Exception e) {
//			throw new CapitalApplicationException(
//					CapitalRespCode.NORMAL_ENDDAY, "转换日期出错!");
//		}
//		saleCapitalDetail.setTranTime(curTime);
//		saleCapitalDetail.setTranType(SaleCapitalDetail.TRANTYPE_ADJUST);
//		saleCapitalDetail.setTicketId(saleCreditAdjustBillId);
//		saleCapitalDetail.setCreditDeposit(obj.getAmount());
//		saleCapitalDetail.setOperId(obj.getOperId());
//		saleCapitalDetail.setRemark(obj.getRemark());
//		saleCapitalDetailDaoImpl.insertSaleCapitalDetail(saleCapitalDetail);
//		// 日志记录
//		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
//				SysOperLog.operType_Add, "添加销售机构信用度调帐单", "SaleBranchNo:"
//						+ saleCapitalDetail.getSaleBranchNo());
//
//	}
//
//}
